from typing import List, Any, Tuple


class Solution:
    def fullJustifyCore(self, words: List[str], maxWidth: int, i: int) -> Tuple[Any]:
        result_list = [] # 结果列表
        result_list_len = 0
        count = 0
        for word in words[i:]:
            result_list.append(word)
            result_list_len += len(word)
            if (result_list_len+count) > maxWidth:
                result_list.pop()
                break
            else:
                count += 1
                result_list.append("")
                
        result = "".join(result_list)
        if count == 1:
            return (i, count, result + " " * (maxWidth - len(result)))
        
        j = 0
        result_len = len(result)
        while result_len < maxWidth:
            if j == count - 1:
                j = 0
                if (i + count) >= len(words):
                    result = "".join(result_list)
                    result_list.append(" " * (maxWidth - len(result)))
                    break
                continue
            else:
                j += 1
                result_len += 1
                result_list[j * 2 - 1] += " "
        
        return (i, count, ''.join(result_list))
    
    def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
        i = 0
        count = 0
        result = []
        print(len(words))
        while (i + count) < len(words):
            (i, count, l) = self.fullJustifyCore(words, maxWidth, i + count)
            print(i, count)
            result.append(l)
        print(result)
        return result


def main():
    sol = Solution()
    print(sol.fullJustify(words=["What", "must", "be", "acknowledgment", "shall", "be"], maxWidth=16))


if __name__ == "__main__":
    main()
